<?php

if (!defined('_PAGE_'))
    include _FLEP_LIB_DIR_.'/page.inc';

$css = array();
$javascript = array();

$structure = flep_page_init('curso');

if(!function_exists('include_page')){
  function include_page($structure) {
    include_curso($structure);
  }
}

function include_curso($structure){
$user = &$_SESSION['user'];
if(isset($structure['entradas']))
  $entradas = &$structure['entradas'];

flep_structure($entradas, 'entrada');
$resultados = isset($structure['search']) && $structure['search'] !== '';

if(isset($_SESSION['entrada_id'])) {
  $entradas['where'][0] = 'entradas.id='.$_SESSION['entrada_id'];
  unset($_SESSION['entrada_id']);
}
if(isset($_POST['id'])) {
  $entradas['where'][0] = 'entradas.id='.flep_decode($_POST['id']);
}
if(isset($_POST['program'])) {
  $entradas['where'][0] = 'programa_id='.flep_decode($_POST['program']);
}

if(!flep_search_all($entradas)) {
echo $entradas['msg'];
    $entradas['rows'] = 1;
    unset($entradas['where']);
    if(flep_search_all($entradas)) {
      $structure['where'][0] = 'entrada_id='.$entradas['data'][0]['id'];
    }
} else {
  $structure['where'][0] = 'entrada_id='.$entradas['data'][0]['id'];
}
if(isset($_SESSION['curso_id'])) {
  $structure['where'][0] = 'cursos.id='.$_SESSION['curso_id'];
  $resultados = true;
  unset($_SESSION['curso_id']);
}

?>
    <b class="title"><?php echo ($resultados ? 'Resultados' : 'Entrada '.flep_get($entradas, 'entrada'));?><?php ?></b>
    <br><br>
    <label onclick="flep_add_curso( null, '<?php echo flep_encode($entradas['data'][0]['id']); ?>')">Nuevo Curso</label>
    <br><br>
    <div class="menu">
        <?php flep_page_search('curso', 'div_curso'); ?>
    </div>
    <br>
    <?php if(!$resultados) {?>
    <table class="header">
        <tbody>
            <tr><th>Fecha de Inicio:</th><td><?php echo flep_get($entradas, 'fecha_inicio');?></td><th>Programa:</th><td><?php echo flep_get($entradas, 'programa');?></td></tr>
            <tr><th>fecha de Cierre:</th><td><?php echo flep_get($entradas, 'fecha_cierre');?></td><td colspan="2">&nbsp;</td></tr>
            <tr><th colspan="4">Descripcion:</th></tr>
            <tr><th colspan="4"><?php echo flep_get($entradas, 'descripcion');?></th></tr>
        </tbody>
    </table>
    <br>
    <?php } ?>
    <table>
    <tr>
    <td>
      <b><?php
        flep_page_message();
        flep_page_navigation($structure);
    ?></b>
    </td><td>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2">
        <br>
    <table id="list" class="list">
        <thead><tr><th>N&deg;</th>
        <?php if($resultados) { ?>
          <th>Entrada</th>
          <th>Fecha Inicio</th>
          <th>fecha de Cierre</th>
        <?php
        }
            foreach($structure['fields'] as &$field){
                if(!array_key_exists('skip',$field))
                  printf('<th>%s</th>', $field['title']);
            }
            unset($field);
            if(flep_get($user, 'administrador') === '1') {
        ?>
        <th></th>
        <th></th>
        <?php } ?>
        <th></th>
        <th></th>
        </tr></thead>
        <tfoot>
            <tr><th colspan="<?php echo count($structure['fields'])+(flep_get($user, 'administrador') === '1'?($resultados?7:4):($resultados?5:3));?>"></th></tr>
        </tfoot>
        <tbody><?php
            $structure['limit'] = null;
            if(flep_search_all($structure)){
                $td_style = '<td style="text-align:center;padding:0px 5px;">';
                for($i = 0; $i < count($structure['data']); $i++){
                    $enc_id = flep_encode($structure['data'][$i]['id']);
                    printf('<tr><td>%s</td>', $structure['offset'] + $i+1);
                    if($resultados) {
                      printf('<td>%s</td><td>%s</td><td>%s</td>', $structure['data'][$i]['entrada'], $structure['data'][$i]['fecha_inicio'], $structure['data'][$i]['fecha_cierre']);
                    }
                    printf('<td>%s</td>', $structure['data'][$i]['nombre']);
                    printf('<td>%s</td>', $structure['data'][$i]['nivel']);
                    printf('<td>%s</td>', $structure['data'][$i]['horario']);
                    printf('<td>%s</td>', $structure['data'][$i]['idioma']);
                    printf('%s<img src="img/inscripcion.png" onclick="flep_inscribir_curso(\'%s\')" title="Incripcion"></td>', $td_style, $enc_id);
                    printf('%s<a href="?page=entrada&curso=%s"><img src="img/details.png" title="Detalles"></a></td>', $td_style, $enc_id);
                    if(flep_get($user, 'administrador') === '1') {
                      printf('%s<img src="img/edit.png" onclick="flep_add_curso(\'%s\')" title="Editar"></td>', $td_style, $enc_id);
                      printf('%s<img src="img/delete.png" onclick="flep_delete(\'curso\', \'%s\', this.parentNode)" title="Eliminar"></td>', $td_style, $enc_id);
                    }
                }
            }else{
                printf('<tr><td colspan="%s">No se encontraron cursos.</td></tr>', count($structure['fields'])+4);
            }
        ?></tbody>
    </table>
            </td></tr>
  </table>
<?php
}

?>
